package cake.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import cake.dao.CategoryDao;
import cake.entity.Category;

public class CategoryDaoImpl implements CategoryDao {
	private JdbcTemplate jt = new JdbcTemplate();

	private static class CategoryDaoRowMapper implements RowMapper<Category> {
		public Category mapRow(ResultSet rs) throws SQLException {
			return new Category(rs.getInt("id"), rs.getString("name"));
		}
	}

	@Override
	public boolean addCategory(Category category) {
		String sql="insert into category(name) values(?)";
		return jt.update(sql,category.getName());
	}

	@Override
	public boolean updateCategory(Category category,int id) {
		String sql="update category set name=? where id=?";
		return jt.update(sql,category.getName(),id);
	}

	@Override
	public boolean deleteCategory(int id) {
		String sql="delete from category where id=?";
		return jt.update(sql, id);
	}

	@Override
	public List<Category> getAllCategory() {
		String sql="select * from category";
		return jt.query(sql, new CategoryDaoRowMapper());
	}

	@Override
	public Category getCategoryById(int id) {
		String sql="select * from category where id=?";
		return jt.queryOne(sql, new CategoryDaoRowMapper(),id);
	}
}
